using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class UImanager : MonoBehaviour
{
    private GameObject Gate;
    public ParameterPlayer parameterPlayerUI = new ParameterPlayer();
    public Text CoinNum, HP, Armor, MP;
    public Image HPbar, Armorbar, MPbar, Skillbar;
    public GameObject Menuingame;
    public GameObject UIingame;
    public GameObject MainMenu;
    public GameObject Board;
    public GameObject gameover;
    public GameObject Dontdestory;
    public Text level;
    private bool playerisalive=true;
    // Start is called before the first frame update
    private void Awake()
    {
        MainMenu.SetActive(false);
        UIingame.SetActive(true);
        Board.SetActive(false);
        Time.timeScale = 0f;
    }
    void Start()
    {
            Gate = GameObject.FindWithTag("Gate");
            parameterPlayerUI = Gate.GetComponent<Gate>().parameterPlayerCopy;
    }

    // Update is called once per frame
    void Update()
    {
        if (playerisalive) { Show();}
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Stop();
        }
        if (parameterPlayerUI.NowHP <= 0&& playerisalive)
        {
            parameterPlayerUI.NowHP = 0;
            playerisalive = false;
            Invoke("Death", 2f);
        }
    }
    void Show()
    {
        if (parameterPlayerUI.NowHP > parameterPlayerUI.MaxHP) { parameterPlayerUI.NowHP = parameterPlayerUI.MaxHP; }
        if (parameterPlayerUI.NowArmor > parameterPlayerUI.MaxArmor) { parameterPlayerUI.NowArmor = parameterPlayerUI.MaxArmor; }
        if (parameterPlayerUI.NowMP > parameterPlayerUI.MaxMP) { parameterPlayerUI.NowMP = parameterPlayerUI.MaxMP; }
        CoinNum.text = parameterPlayerUI.coin.ToString();
        level.text = "1-"+SceneManager.GetActiveScene().buildIndex.ToString();
        HP.text = parameterPlayerUI.NowHP.ToString() + "/" + parameterPlayerUI.MaxHP.ToString();
        Armor.text = parameterPlayerUI.NowArmor.ToString() + "/" + parameterPlayerUI.MaxArmor.ToString();
        MP.text = parameterPlayerUI.NowMP.ToString() + "/" + parameterPlayerUI.MaxMP.ToString();
        HPbar.transform.localScale = new Vector3((float)parameterPlayerUI.NowHP / parameterPlayerUI.MaxHP, 1, 1);
        Armorbar.transform.localScale = new Vector3((float)parameterPlayerUI.NowArmor / parameterPlayerUI.MaxArmor, 1, 1);
        MPbar.transform.localScale = new Vector3((float)parameterPlayerUI.NowMP / parameterPlayerUI.MaxMP, 1, 1);
        HPbar.transform.localScale = new Vector3((float)parameterPlayerUI.NowHP / parameterPlayerUI.MaxHP, 1, 1);
        if (parameterPlayerUI.timerForskillTime == 0)
        { Skillbar.transform.localScale = new Vector3((float)parameterPlayerUI.timerForskillCD / parameterPlayerUI.skillCD, 1, 1); }
        else { Skillbar.transform.localScale = new Vector3((float)parameterPlayerUI.timerForskillTime / parameterPlayerUI.skillTime, 1, 1); }
    }
    public void Stop()
    {
        if (Time.timeScale == 1)
        {
            Time.timeScale = 0;
            Menuingame.SetActive(true);
            Board.SetActive(false);
        }
        else if(Time.timeScale == 0)
        {
            Time.timeScale = 1f;
            Menuingame.SetActive(false);
            Board.SetActive(true);
        }
    }
    public void Newgame()
    {
        SceneManager.LoadScene(1);
        Destroy(Dontdestory);
    }
    public void Restart()
    {
        SceneManager.LoadScene(0);
        Destroy(Dontdestory);
    }
    public void Exit()
    {
        #if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
        #else
        Application.Quit();
        #endif
    }
    public void Death()
    {
        Destroy(GameObject.FindWithTag("Player"));
        MainMenu.SetActive(false);
        Board.SetActive(false);
        gameover.SetActive(true);
    }
}
